package db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.swing.JOptionPane;

public class DBManager
{

	private static DBManager uniqueInstance = null;
	private static Connection con = null;

	private static String drivers;
	private static String url;

	public static void setParameters(String drivers, String url)
	{
		DBManager.drivers = drivers;
		DBManager.url = url;
	}

	private DBManager()
	{

		if (!dbExists())
		{
			System.err.println("de database bestaat niet....");
		}
	}

	public static synchronized DBManager getInstance()
	{
		if (uniqueInstance == null)
		{
			uniqueInstance = new DBManager();
		}
		return uniqueInstance;
	}

	private Boolean dbExists()
	{
		Boolean exists = false;

		try
		{
			con = DriverManager.getConnection(url);
			exists = true;
			System.out.println("Connectie met Database gemaakt");
		} catch (Exception e)
		{
			JOptionPane.showMessageDialog(null, e.getMessage(), "", JOptionPane.ERROR_MESSAGE);
			exists = false;
		}
		return (exists);
	}

	public void close()
	{
		try
		{
			con.close();
			uniqueInstance = null;
			con = null;
		} catch (SQLException e)
		{
			e.printStackTrace();
		}
	}

	/**
	 * returnt de huidige verbinding
	 * 
	 * @return de verbinding
	 */
	public Connection getConnection()
	{
		return con;
	}
}